﻿using Ins.Packaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO.Packaging;
using System.Xml;

namespace Ins.Office.Testing
{
    
    
    /// <summary>
    ///This is a test class for PackTest and is intended
    ///to contain all PackTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PackTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for ExtractPart
        ///</summary>
        [TestMethod()]
        public void ExtractPartTest()
        {
            using (Package pkg = Package.Open(@"C:\Data\Office\test1.xlsx", System.IO.FileMode.Open))
            {
                PackageSettings settings = new PackageSettings();
                settings.PackageRelationshipType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
                Pack p = new Pack(pkg, settings);
                string partPath = @"worksheets/sheet1.xml";
                string relType = @"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet";
                PackagePart docPart = p.GetDocumentPart();
                PackagePart part = p.GetPart(docPart, relType, partPath);
                string targetDirectory = @"C:\Data\Office\output\";
                Pack.ExtractPart(part, targetDirectory);
                Assert.Inconclusive("A method that does not return a value cannot be verified.");
            }
        }

        /// <summary>
        ///A test for DeletePart
        ///</summary>
        [TestMethod()]
        public void DeletePartTest()
        {
            using (Package pkg = Package.Open(@"C:\Data\Office\test1.xlsx", System.IO.FileMode.Open))
            {
                PackageSettings settings = new XLPackageSettings();
                Pack p = new Pack(pkg, settings);
                string sheetCodeName = "Sheet3";
                string partPath = @"worksheets/" + sheetCodeName + ".xml";
                string relType = XLPackageSettings.WorksheetRelationType;
                PackagePart docPart = p.GetDocumentPart();
                PackagePart part = p.GetPart(docPart, relType, partPath);
                bool expected = true;
                bool actual;
                actual = false; //p.DeletePart(docPart, relType, partPath);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(docPart.GetStream());
                XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
                ns.AddNamespace("x", XLPackageSettings.DefaultNS);
                ns.AddNamespace("r", XLPackageSettings.RelationshipNS);
                var nodes = xmlDoc.SelectNodes(String.Format("//x:sheet[@name='{0}']", sheetCodeName), ns);
                foreach (XmlNode node in nodes)
                {
                    XmlNode pnode = node.ParentNode;
                    pnode.RemoveChild(node);
                }
                xmlDoc.Save(docPart.GetStream());
                Assert.AreEqual(expected, actual);
            }
        }
    }
}
